-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Various fixes for manual_is_power_of_two
#14463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
d0d7a23
to
abde06c
Compare
r? clippy |
This is a refactoring of the existing code to remove repetitive code.
Since the fixed expression is used as a receiver for `.is_power_of_two()`, it may require parentheses.
abde06c
to
9659649
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great improvements to the lint! I've marked some slight nits
); | ||
/// Return the unsigned integer receiver of `.count_ones()` | ||
fn count_ones_receiver<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> { | ||
if let ExprKind::MethodCall(method_name, receiver, [], _) = expr.kind |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could also check for <int>::count_ones(x)
, it's a normal ExprKind::Call
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. Done, and tested, in a new commit on top of the others.
if let ExprKind::Binary(op, lhs, rhs) = smaller.kind | ||
&& !lhs.span.from_expansion() | ||
&& !rhs.span.from_expansion() | ||
&& op.node == BinOpKind::Sub |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move this op.node == BinOpKind::Sub
to above the expansion checking?
Also, thinking about checking for expansion so many times, we could probably minimize some of these (as we check for expansions 3 times before linting)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! All checks are necessary, but I've factored them in through a helper function. This makes code easier to read. The modified commit is Do not lint result from macro expansion.
If parts of the expression comes from macro expansion, it may match an expression equivalent to `is_power_of_two()` by chance only.
9659649
to
c43c87d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks! ❤️ 🌈
Fix #14461:
const
contextCommits have been logically separated to facilitate review, and start with a refactoring (and simplification) of the existing code.
changelog: [
manual_is_power_of_two
]: insert parentheses as required in suggestion, check MSRV before suggesting fix inconst
context, do not lint macro expansion results